<?php
/**
 * ResMania
 * More Information contact: info@resmania.com
 *
 * @package Joomla - ResMania Router
 * @version 0.5
 * @author webformatique
 * @copyright (C) 2010- webformatique
 * @license GNU/GPLv3 http://www.gnu.org/copyleft/gpl.html
 */

// no direct access
defined('_JEXEC') or die('Restricted access');

class RM_Joomla_Controller_Router extends RM_Controller_Router_Rewrite {
    public function _($controller = '', $action = '', $options = array()){
        if (isset($_REQUEST['Itemid'])) {
            $options['Itemid'] = $_REQUEST['Itemid'];
        }
        $userParams = array_merge($options, array('act' => $controller, 'task' => $action));
        return $this->assemble($userParams);
    }

    public function assemble($userParams, $name = null, $reset = false, $encode = true)
    {
        if (RM_Joomla_Connector::is16() && (
            (isset($userParams['index3']) && $userParams['index3'] == true) ||
            (isset($userParams['index2']) && $userParams['index2'] == true)
        )){
            $userParams['format'] = 'row';
        }
        $url = parent::assemble($userParams, $name, $reset, $encode);

        if (RM_Joomla_Connector::is16()) {
            $url = 'index.php'.$url;
        } elseif (RM_Joomla_Connector::is15()) {
            if (isset($userParams['index3']) && $userParams['index3'] == true){
                $url = 'index3.php'.$url;
            } else if (isset($userParams['index2']) && $userParams['index2'] == true){
                $url = 'index2.php'.$url;
            } else {
                $url = 'index.php'.$url;
            }
        }

        if ($this->_module == 'admin') {
            $url = JURI::root().'administrator/'.$url;
        } else {
            $url = JURI::root().$url;
        }
        return $url;
    }    
}